$(document).ready(function(){
	//加载下拉框
	getProvinceList();
});

function getProvinceList(){
	
	$.ajax({
   		async:false,
		url:'/admin/org!getProvinceList.action',
		type:'POST',
		dataType:'json',
		cache: false,
		success:function(data){
			Interceptor(data);
			var resultContent='';
			$(data).each(function(i) {
				resultContent+='<option value="'+data[i].code+'">'+data[i].name+'</option>';
			});
			$("#province").html(resultContent);
			
			getOrgInfo();
		}
	});
}

function getOrgInfo() {
	$.ajax({
   		async:false,
		url:'/admin/org!findUserOrgById.action',
		type:'POST',
		dataType:'json',
		cache: false,
		success:function(data){
			Interceptor(data);
			var sjreson="";
			var baocunIdStr="";
			
			if(data!=null){
				var dataObj=data.org;
				$("#id").val(dataObj.ID);
				$("#name").val(dataObj.name);
				$("#organType").val(dataObj.organType);
				$("#organLevel").val(dataObj.organLevel);
				$("#organCode").val(dataObj.organCode);
				$("#provinceName").val(dataObj.province.name);
				
				$("#address").val(dataObj.address);
				$("#postCode").val(dataObj.postCode);
				$("#contactUser").val(dataObj.contactUser);
				$("#contactPhone").val(dataObj.contactPhone);
				$("#contactMobile").val(dataObj.contactMobile);
				$("#contactEmail").val(dataObj.contactEmail);
				//$("#province").val(dataObj.province.code);
				$("#responsiblePerson").val(dataObj.responsiblePerson);
				
				//2016-06-07 17:32:49
				$("#institutionEthicsCommitteeName").val(dataObj.institutionEthicsCommitteeName);
				$("#institutionEthicsCommitteePersonnel").val(dataObj.institutionEthicsCommitteePersonnel); 
				if(dataObj.institutionEthicsCommitteeConstitution==1) {
					$("#institutionEthicsCommitteeConstitution_1").attr("checked",true);	
				}else if(dataObj.institutionEthicsCommitteeConstitution==0) {
					$("#institutionEthicsCommitteeConstitution_0").attr("checked",true);
				}
				
				if(dataObj.institutionEthicsCommitteeProgram==1) {
					$("#institutionEthicsCommitteeProgram_1").attr("checked",true);	
				}else if(dataObj.institutionEthicsCommitteeProgram==0) {
					$("#institutionEthicsCommitteeProgram_0").attr("checked",true);
				}
				
			}
			
		},
		error:function(){
			//nexts(tag);
		}
	});
}

function submitForm() {
	var name = $("#name").val();
	var organType = $("#organType").val();
	var organLevel = $("#organLevel").val();
	var organCode = $("#organCode").val();
	var address = $("#address").val();
	var postCode = $("#postCode").val();
	var contactUser = $("#contactUser").val();
	var contactPhone = $("#contactPhone").val();
	var contactMobile = $("#contactMobile").val();
	var contactEmail = $("#contactEmail").val();
	//var province = $("#province").val();
	var responsiblePerson = $("#responsiblePerson").val();
	//
	var institutionEthicsCommitteeName = $("#institutionEthicsCommitteeName").val();
	var institutionEthicsCommitteePersonnel = $("#institutionEthicsCommitteePersonnel").val();
	var institutionEthicsCommitteeConstitution = $('input[name="institutionEthicsCommitteeConstitution"]:checked').val();
	var institutionEthicsCommitteeProgram = $('input[name="institutionEthicsCommitteeProgram"]:checked').val();
	
	$('span[id^="note_"]').html('');
	
	if($.trim(name)==''){
	  	$("#note_name").html(' <font style="color:red;">机构名称不可以为空</font>');
	  	$("#name").focus();
		return false;
	}
	if($.trim(organType)==''){
	  	$("#note_organType").html(' <font style="color:red;">机构类型不可以为空</font>');
	  	$("#organType").focus();
		return false;
	}
	if($.trim(organLevel)==''){
	  	$("#note_organLevel").html(' <font style="color:red;">机构等级不可以为空</font>');
	  	$("#organLevel").focus();
		return false;
	}
	if($.trim(organCode)==''){
	  	$("#note_organCode").html(' <font style="color:red;">机构编码不可以为空</font>');
	  	$("#organCode").focus();
		return false;
	}
	if($.trim(address)==''){
	  	$("#note_address").html(' <font style="color:red;">地址不可以为空</font>');
	  	$("#address").focus();
		return false;
	}
	if($.trim(postCode)==''){
	  	$("#note_postCode").html(' <font style="color:red;">邮编不可以为空</font>');
	  	$("#postCode").focus();
		return false;
	}
	if($.trim(contactUser)==''){
	  	$("#note_contactUser").html(' <font style="color:red;">联系人不可以为空</font>');
	  	$("#contactUser").focus();
		return false;
	}
	if($.trim(responsiblePerson)==''){
	  	$("#note_responsiblePerson").html(' <font style="color:red;">机构负责人不可以为空</font>');
	  	$("#responsiblePerson").focus();
		return false;
	}
	if($.trim(contactPhone)==''){
	  	$("#note_contactPhone").html(' <font style="color:red;">联系人电话不可以为空</font>');
	  	$("#contactPhone").focus();
		return false;
	}
	if($.trim(contactMobile)==''){
	  	$("#note_contactMobile").html(' <font style="color:red;">联系人手机不可以为空</font>');
	  	$("#contactMobile").focus();
		return false;
	}
	if($.trim(contactEmail)==''){
	  	$("#note_contactEmail").html(' <font style="color:red;">联系人邮箱不可以为空</font>');
	  	$("#contactEmail").focus();
		return false;
	}
	
	$.ajax({
   		async:false,
		url:'/admin/org!saveOrgWeiHu.action',
		type:'post',
		data:{
			"name": name,
			"organType": organType,
			"organLevel": organLevel,
			"organCode": organCode,
			"address": address,
			"postCode": postCode,
			"contactUser": contactUser,
			"contactPhone": contactPhone,
			"contactMobile": contactMobile,
			"contactEmail": contactEmail,
			"responsiblePerson": responsiblePerson,
			"institutionEthicsCommitteeName": institutionEthicsCommitteeName,
			"institutionEthicsCommitteePersonnel": institutionEthicsCommitteePersonnel,
			"institutionEthicsCommitteeConstitution": institutionEthicsCommitteeConstitution,
			"institutionEthicsCommitteeProgram": institutionEthicsCommitteeProgram
		},
		dataType:'Text',
		cache:false,
		success:function(data){
			Interceptor(data);
			if(data!=null && data=="success"){
				alert("保存成功!");
			}else if(data=="error"){
				alert("保存失败!");
			}
		},
		error:function(error){
			alert("通信失败!");
		}
	}); 
}

